"ui";
//importClass(android.graphics.Color);
//importClass(android.content.res.ColorStateList);
importClass(android.view.View);
ui.statusBarColor(colors.TRANSPARENT);
//activity.window.addFlags(android.view.WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
//使用adb授权无障碍
importClass(android.content.Context);
importClass(android.provider.Settings);
try {
    var enabledServices = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES);
    log('当前已启用的辅助服务\n', enabledServices);
    var Services = enabledServices + ":org.autojs.autojspro/com.stardust.autojs.core.accessibility.AccessibilityService";
    Settings.Secure.putString(context.getContentResolver(), Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES, Services);
    Settings.Secure.putString(context.getContentResolver(), Settings.Secure.ACCESSIBILITY_ENABLED, '1');
    toastLog("成功开启AutoJS的辅助服务");
} catch (error) {
    //授权方法：开启usb调试并使用adb工具连接手机，执行 adb shell pm grant org.autojs.autojspro android.permission.WRITE_SECURE_SETTING
    toastLog("\n请确保已给予 WRITE_SECURE_SETTINGS 权限\n\n授权代码已复制，请使用adb工具连接手机执行(重启不失效)\n\n", error);
    setClip("adb shell pm grant org.autojs.autojspro android.permission.WRITE_SECURE_SETTINGS");
}
ui.layout(
    <frame bg="#ffffff">
        <img src="https://s1.ax1x.com/2023/02/15/pS7qSYQ.jpg" scaleType="fitXY" h="*"/>
        <canvas id="board" alpha="0.5" layout_weight="1"/>
        <frame h="*">
        <vertical layout_gravity="center|bottom" gravity="center|bottom">
            <progressbar id="progressbar" indeterminate="true" style="@style/Base.Widget.AppCompat.ProgressBar.Horizontal" />
            <text id="Maple_author" margin="1 6" textColor="#000000" gravity="center|bottom" textSize="11">NiXiaoXiuService</text>
        </vertical>
    </frame>
    </frame>
)

var SystemUiVisibility = ve => {
    var option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | (ve ? View.SYSTEM_UI_FLAG_LAYOUT_STABLE : View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
    activity.getWindow().getDecorView().setSystemUiVisibility(option);
};
SystemUiVisibility(false);

//画笔
var paint = new Paint();
paint.setStrokeWidth(2);
字母 = String.fromCharCode(111); //20起步
存储 = []
x = 100
y = 100

ui.board.on("draw", function(canvas) {
    paint.setColor(colors.parseColor("#ffffff"));
    canvas.drawPaint(paint); //清画布
    paint.setColor(colors.parseColor("#ffc3c3c0"));
    paint.setFakeBoldText(true); //字体加粗
    paint.setTextSize(50); //字体大小
    //函数(x,y,canvas);
    数量 = 存储.length
    if (数量 != 20) {
        存储.push(random(50, 900))
        存储.push(random(50, 900))
        存储.push(0)
    }
    //canvas.drawText(存储[2],80,80, paint);
    for (var i = 0; i < 数量; i += 3) {
        for (var s = 0; s < 3; s++) {
            canvas.drawText(字母, 存储[i], 存储[i + 1] + 35 * s + 存储[i + 2], paint);
            存储.splice(i + 2, 1, 存储[i + 2] + 30)
        }
    }
    for (var i = 0; i < 数量; i += 3) {
        if (存储[i + 2] > 2000) {
            存储.splice(i, 3)
        }

    }
    //sleep(1000)
});

var degree = {
    x: 0,
    y: 0,
    z: 0
}
var offset = {
    x: 0,
    y: 1000
};
var star = new Array();
var count = 200
var size = 2000
ui.board.setMaxFps(60)
for (let i = 0; i < count; i++) {
    star.push(Math.random() * size)
    star.push(Math.random() * 360)
    star.push(colors.rgb(Math.random() * 255, 0, 255))
}
var paint = new Paint();
ui.board.on("draw", function(canvas) {
    //绘制背景色
    canvas.drawColor(colors.argb(20, 0, 0, 0));
    //绘制分数
    paint.setColor(colors.BLACK);
    paint.setTextSize(50);
    //canvas.drawText(n);
    //toast(JSON.stringify(degree,null,4))
    paint.setStrokeWidth(1);
    paint.setStrokeCap(Paint.Cap.ROUND);


    //  offset.x=(offset.x+degree.y*200+540)/2
    //  offset.y=(offset.y+degree.x*300+1000)/2
    //偏移坐标
    canvas.translate(offset.x, offset.y);
    //绘制围墙
    //toast(JSON.stringify(cube.p1,null,4))
    paint.setStrokeWidth(2)
    galaxy(canvas, paint);
});

//连线
function line(canvas, paint, x1, y1, x2, y2) {
    canvas.drawLine(x1, y1, x2, y2, paint);
}

function galaxy(canvas, paint) {
    for (let i = 0; i < count; i++) {
        paint.setColor(star[i * 3 + 2]);
        paint.setStrokeWidth(Math.random() * 0 + 10)
        canvas.drawPoint(star[i * 3] * Math.cos(star[i * 3 + 1]), star[i * 3] * Math.sin(star[i * 3 + 1]), paint)
        paint.setStrokeWidth(Math.random() * 3 + 3)

        paint.setColor(colors.WHITE);
        canvas.drawPoint(star[i * 3] * Math.cos(star[i * 3 + 1]), star[i * 3] * Math.sin(star[i * 3 + 1]), paint)
        //star[i*2+1]+=(size-star[i*2])/20000-0.025
        star[i * 3 + 1] += 0.0015
    }
}

function point(canvas, paint, x1, x2) {
    //if(x1>
}
sensors.register("gyroscope", sensors.delay.game).on("change", (event, azimuth, pitch, roll) => {
    degree.x = azimuth;
    degree.y = pitch;
    if (pitch < -90) {
        a = -roll - 180
    } else if (pitch > 90) {
        a = -roll + 180
    } else {
        a = roll
    }
    degree.z = a
});


//启动文件
threads.start(function() {
    const CONFIG = {
        "path": "AutoJsPro热更新/", //脚本文件路径
        "scriptName": "main.js", //脚本名称
        "user": "1833063608@qq.com", //用户名
        "key": "a6danburmweh5rsu" //应用秘钥
    }
    //threads.start(function(){while(true){toastLog("请稍等...脚本加载中...");sleep(4000);}});
    try {
        http.__okhttp__.setTimeout(20000);
        _res = http.get('http://dav.jianguoyun.com/dav/' + CONFIG.path + CONFIG.scriptName, {
            headers: {
                "Authorization": "Basic " + java.lang.String(android.util.Base64.encode(java.lang.String(CONFIG.user + ':' + CONFIG.key).getBytes(), 2)),
                "Content-Type": "text/plain;charset=UTF-8",
            }
        }).body.string();
        threads.shutDownAll();
        engines.execScript(CONFIG.scriptName, _res);
    } catch (error) {
        alert("加载超时,请重启脚本~~~");
    }
});

